package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginResultVo login(User user) {
        User u = userMapper.SelectByUsername(user.getUsername());
        //加密密码
        String pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        /*user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));*/
        if (u == null){
            log.info("用户不存在");
            throw new BusinessException("用户名或密码错误");
        }
        if (!u.getPassword().equals(pass)){
            log.info("密码错误");
            throw new BusinessException("用户名或密码错误");
        }
        //判断状态是否停用
        if (u.getStatus() == 0){
            log.info("用户状态已停用");
            throw new BusinessException("用户状态已停用");
        }

        return new LoginResultVo(u.getId(), u.getUsername(), u.getName(), u.getImage(), u.getRoleName(), u.getToken());
    }

    @Override
    public PageResult<User> list(UserDto userDto) {
        //设置分页参数
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        //调用mapper查询
        List<User> userList = userMapper.selectList(userDto);
        //强转类型为page,调用page方法
        Page<User> p = (Page<User>) userList;
        return new PageResult<>(p.getTotal(),p.getResult());

    }

    @Override
    public void deleteUser(List <Integer> ids) {
        userMapper.deleteById(ids);
    }

    @Override
    public void addUser(User user) {
        //补全基本属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        String password = user.getUsername() + "123";
        //密码加密
        user.setPassword (DigestUtils.md5DigestAsHex(password.getBytes())) ;
        userMapper.addUser(user);
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public void updateUser(User user) {
        String password = user.getUsername() + "123";
        user.setPassword (DigestUtils.md5DigestAsHex(password.getBytes())) ;
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }

    @Override
    public List<User> getUserByRole(String roleLabel) {
        return userMapper.getUserByRole(roleLabel);
    }

    @Override
    public List<User> getUserByDept(Integer deptId) {
        return userMapper.getUserByDept(deptId);
    }


}
